/**
 * @param {number[]} coins
 * @param {number} amount
 * @return {number}
 */
var coinChange = function (coins, amount) {
  coins.sort((a, b) => a - b)
  let arr = [], res = 0
  for (let i = coins.length - 1; i >= 0;) {
    while (res < amount) {
      res += coins[i]
      arr.push(coins[i])
    }
    if (res == amount) return arr.length
    else {
      res -= coins[i]
      arr.pop()
      i--
    }

  }
  if (res != amount) return -1
};
console.log(coinChange([186, 419, 83, 408],
  6249));
